package com.x.uikit.toolbar;

import android.content.Context;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.x.uikit.link.XLink;
import com.x.uikit.link.XParams;

import java.util.HashMap;
import java.util.List;

public class XToolbar extends LinearLayout implements XLink.EventObserver, XLink.CommandHandler {
    protected HashMap<String, XToolbarItem> mItems = new HashMap<>();
    protected OnItemClickListener mItemClickListener;
    protected OnItemLongClickListener mItemLongClickListener;
    protected List<String> mTokens;
    protected XLink.EventObserver mEventObserver;
    protected IItemLayoutParamsGenerator mItemLayoutParamsGenerator = new ItemLayoutParamsGeneratorImpl();
    protected IItemCreator mItemCreator;

    public XToolbar(Context context) {
        super(context);
    }

    public void setEventObserver(XLink.EventObserver observer) {
        this.mEventObserver = observer;
    }

    public void setItemLayoutParamsGenerator(IItemLayoutParamsGenerator generator) {
        if (generator == null) {
            return;
        }
        this.mItemLayoutParamsGenerator = generator;
    }

    public void setItemCreator(IItemCreator creator) {
        mItemCreator = creator;
    }

    public void setItems(List<String> tokens) {
        if (mItemCreator == null) {
            throw new IllegalArgumentException("item creator is null,set item creator first");
        }
        if (tokens == null || tokens.isEmpty() || mTokens == tokens) {
            return;
        }
        mItems.clear();
        removeAllViews();
        for (final String token : tokens) {
            final XToolbarItem item = mItemCreator.createItem(token, getContext());
            if (item != null) {
                item.setItemClickListener(new XToolbarItem.IClickListener() {
                    @Override
                    public void onClick(XToolbarItem item) {
                        onItemClicked(token, item);
                    }

                    @Override
                    public void onLongClick(XToolbarItem item) {
                        onItemLongClicked(token, item);
                    }
                });
                item.setEventObserver(this);
                mItems.put(token, item);
                ViewGroup.LayoutParams lp = item.getLayoutParams();
                if (lp == null) {
                    lp = mItemLayoutParamsGenerator.generateLayoutParams(token);
                }
                this.addView(item, lp);
            }
        }

    }


    public XToolbarItem findItem(String token) {
        return mItems.get(token);
    }

    public <T extends XToolbarItem> T findItem(String token, Class<T> clazz) {
        XToolbarItem item = mItems.get(token);
        if (clazz.isInstance(item)) {
            return (T) item;
        }
        return null;
    }

    protected void onItemLongClicked(String token, XToolbarItem item) {
        if (mItemLongClickListener != null) {
            mItemLongClickListener.onItemLongClicked(token, item);
        }
    }

    protected void onItemClicked(String token, XToolbarItem item) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClicked(token, item);
        }
    }


    public void setItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public void setItemLongClickListener(OnItemLongClickListener listener) {
        this.mItemLongClickListener = listener;
    }

    @Override
    public void onEvent(int eventId, XParams params, XParams result) {
        if (mEventObserver != null) {
            mEventObserver.onEvent(eventId, params, result);
        }
    }


    @Override
    public boolean handCommand(int commandId, XParams params, XParams result) {
        for (String key : mItems.keySet()) {
            XToolbarItem item = mItems.get(key);
            if (item != null && item.handCommand(commandId, params, result)) {
                return true;
            }
        }
        return false;
    }

    public interface OnItemClickListener {
        void onItemClicked(String token, XToolbarItem item);
    }

    public interface OnItemLongClickListener {
        void onItemLongClicked(String token, XToolbarItem item);
    }

    private static class ItemLayoutParamsGeneratorImpl implements IItemLayoutParamsGenerator {
        @NonNull
        @Override
        public LayoutParams generateLayoutParams(String token) {
            LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.gravity = Gravity.CENTER_VERTICAL;
            return layoutParams;
        }
    }

    public interface IItemLayoutParamsGenerator {
        @NonNull
        LayoutParams generateLayoutParams(String token);
    }

    public interface IItemCreator {
        XToolbarItem createItem(String token, Context context);
    }
}
